#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int N = 100010;
int lg[N], f[N][20];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int n = read(), m = read();
  lg[1] = 0;
  for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
  for (int i = 1; i <= n; ++i) f[i][0] = read();  // ST 表原始数据
  for (int j = 1; j <= lg[n]; ++j)
    for (int i = 1; i <= n - (1 << j) + 1; ++i)
      f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
  for (int i = 1; i <= m; ++i) {
    int l, r;
    l = read(), r = read();
    int log2 = lg[r - l + 1];
    printf("%d\n", max(f[l][log2], f[r - (1 << log2) + 1][log2]));
  }
  return 0;
}
